//
//  AWVisitorView.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/5.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit
import SnapKit

class AWVisitorView: UIView {
    
    var registerCallBack: (()->())?
    
    var loginCallBack: (()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
        
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        addSubview(cycleImageView)
        
        addSubview(maskImageView)
        
        addSubview(houseImageView)
        
        addSubview(messageLabel)
        
        addSubview(registerButton)
        
        addSubview(loginButton)
        
        let sliceImage = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsetsMake(5,5,5,5), resizingMode: .stretch)
        
        registerButton.setBackgroundImage(sliceImage, for: .normal)
        
        registerButton.adjustsImageWhenHighlighted = false
        
        loginButton.setBackgroundImage(sliceImage, for: .normal)
        
        loginButton.adjustsImageWhenHighlighted = false
        
        registerButton.addTarget(self, action: #selector(registerButtonDidClicked), for: .touchUpInside)
        
        loginButton.addTarget(self, action: #selector(loginButtonDidClicked), for: .touchUpInside)
        
        cycleImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self.snp.centerY).offset(-40)
            
        }
        
        maskImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
            
        }
        
        houseImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
            
        }
        
        messageLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(cycleImageView.snp.bottom).offset(20)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(220)
            
        }
        
        registerButton.snp.makeConstraints { (make) in
            
            make.right.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
            
        }
        
        loginButton.snp.makeConstraints { (make) in
            
            make.left.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.width.equalTo(registerButton)
            
        }
                
//        setLayoutConstraints()
    }
    
    fileprivate lazy var cycleImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    fileprivate lazy var maskImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    fileprivate lazy var houseImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    fileprivate lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.darkGray, maxWidth: nil)
        
//    {
//    
//        let label = UILabel()
//        
//        label.font = UIFont.systemFont(ofSize: 14)
//        
//        label.textColor = UIColor.darkGray
//        
//        label.text = "关注一些人，回这里看看有什么惊喜"
//        
//        label.textAlignment = .center
//        
//        label.numberOfLines = 0
//        
//        label.preferredMaxLayoutWidth = 220
//        
//        return label
//        
//    }()
    
    fileprivate lazy var registerButton: UIButton = UIButton(title: "注册", normalTextColor: UIColor.darkGray, highlightedTextColor: UIColor.orange, fontSize: 15)
        
//    {
//    
//        let button = UIButton()
//        
//        button.setTitle("注册", for: .normal)
//        
//        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//        
//        button.setTitleColor(UIColor.darkGray, for: .normal)
//        
//        button.setTitleColor(UIColor.orange, for: .highlighted)
//        
//        let sliceImage = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsetsMake(5,5,5,5), resizingMode: .stretch)
//        
//        button.setBackgroundImage(sliceImage, for: .normal)
//        
//        button.adjustsImageWhenHighlighted = false
//        
//        return button
//        
//    }()
    
    fileprivate lazy var loginButton: UIButton = UIButton(title: "登录", normalTextColor: UIColor.darkGray, highlightedTextColor: UIColor.orange, fontSize: 15)
        
//    {
//        
//        let button = UIButton()
//        
//        button.setTitle("登录", for: .normal)
//        
//        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//        
//        button.setTitleColor(UIColor.darkGray, for: .normal)
//        
//        button.setTitleColor(UIColor.orange, for: .highlighted)
//        
//        let sliceImage = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsetsMake(5,5,5,5), resizingMode: .stretch)
//        
//        button.setBackgroundImage(sliceImage, for: .normal)
//        
//        button.adjustsImageWhenHighlighted = false
//        
//        return button
//        
//    }()
    
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        animation.toValue = Double.pi * 2
        
        animation.isRemovedOnCompletion = false
        
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    
    @objc private func registerButtonDidClicked() {
        
        registerCallBack?()
        
    }
    
    @objc private func loginButtonDidClicked() {
        
        loginCallBack?()
        
    }
    
    func updateView(message: String?, imageName: String?) {
        
        if let msg = message, let imgName = imageName {
            
            cycleImageView.isHidden = true
            
            houseImageView.image = UIImage(named: imgName)
            
            messageLabel.text = msg
            
        } else {
            
            startAnimation()
            
        }
        
    }

}

extension AWVisitorView {
    //手写代码添加autolayout
    func setLayoutConstraints() {
        
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        
        let cycleCenterXConstraint = NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        
        let cycleCenterYConstraint = NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
        
        addConstraints([cycleCenterXConstraint, cycleCenterYConstraint])
        
    }
    
}
